<?xml version="1.0"?>
<!-- xsl:stylesheet
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:UML='href://org.omg/UML/1.1'
	xmlns:CWM="org.omg.CWM1.0/ObjectModel" xmlns:UML="org.omg.CWM1.0/Core" xmlns:CWMRDB="org.omg.CWM1.0/Relational"
	version="1.0" -->
<xsl:stylesheet 
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:CWM="org.omg.CWM1.0/ObjectModel" xmlns:UML="org.omg.CWM1.0/Core" xmlns:CWMRDB="org.omg.CWM1.0/Relational"
	version="1.0">
	<xsl:output method="html"  version="4.0" encoding="utf-8" indent="yes"/>
	<xsl:key name="type-index" match="//CWMRDB:SQLSimpleType" use="@xmi.id"/>

	<!--
		This transform summarizes in HTML an XMI 1.1 generated by Business Objects DS 
		(NB! BO DS generats wrong namespaces) 
	-->
	<xsl:template match="/" >
		<html>
			<head>
				<title></title>
				<xsl:call-template name="css" />
			</head>
			<body>
				
		<h1>
			List (Number of tables: <xsl:value-of select="count(//CWMRDB:Table)"/>)
		</h1>
		<ul>
			<xsl:for-each select="//CWMRDB:Table">
				<li>
					<a href="#{@name}">
						<xsl:value-of select="@name"/>
					</a>
					- <xsl:value-of select="UML:ModelElement.taggedValue/UML:TaggedValue[@tag='TechnicalDescription']/@value"/>
				</li>
			</xsl:for-each>
		</ul>

		<xsl:for-each select="//CWMRDB:Table">
			<h2>
				<a name="{@name}">
					<xsl:value-of select="@name"/>
				</a>
			</h2>
			<!-- h3>Technical Description</h3 -->
			<p>
				<xsl:value-of select="UML:ModelElement.taggedValue/UML:TaggedValue[@tag='TechnicalDescription']/@value"/>
			</p>
			<!-- h3>Columns</h3 -->
			<table>
				<thead>
					<tr>
						<th>Name</th>
						<th colspan="2">Type</th>
						<th>Description</th>
					</tr>
				</thead>
				<tbody>
					<xsl:for-each select="CWM:Classifier.feature/CWMRDB:Column">
						<xsl:variable name="id" select="@xmi.id" />
						<xsl:variable name="name" select="@name" />
						<tr>
							<xsl:if test="(position() mod 2) != 1">
								<xsl:attribute name="class">odd</xsl:attribute>
							</xsl:if>
							<td>
								<xsl:choose>
									<xsl:when test="../../CWM:Namespace.ownedElement/CWMRDB:PrimaryKey/CWM:UniqueKey.feature/CWM:StructuralFeature[@xmi.idref=$id]">
										<b>
											<xsl:value-of select="$name"/>
										</b>
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="$name"/>
									</xsl:otherwise>
								</xsl:choose>

							</td>
							<td>
								<i>
									<xsl:variable name="mapped-type" select="key('type-index',@type)" />
 									<xsl:choose>
										<xsl:when test="$mapped-type"><xsl:value-of select="$mapped-type/@name"/></xsl:when>
										<xsl:when test="@type='di_char'">varchar</xsl:when>
										<xsl:when test="@type='di_decimal'">numeric</xsl:when>
										<xsl:when test="contains(@type,'di_')">
											<xsl:value-of select="substring(@type, 4)"/>
										</xsl:when>
										<xsl:otherwise>
											<xsl:value-of select="@type"/>
										</xsl:otherwise>
									</xsl:choose>
									<xsl:if test="@length">
										(<xsl:value-of select="@length"/>)
									</xsl:if>
									<xsl:if test="@precision">
										(<xsl:value-of select="@precision"/><xsl:if test="@scale">
											,<xsl:value-of select="@scale"/>
										</xsl:if>)
									</xsl:if>
								</i>
							</td>
							<td>
								<xsl:if test="@isNullable='columnNullable'">N</xsl:if>
							</td>
							<td>
								<xsl:value-of select="UML:ModelElement.taggedValue/UML:TaggedValue[@tag='TechnicalDescription']/@value"/>
							</td>

						</tr>

					</xsl:for-each>
				</tbody>
			</table>

		</xsl:for-each>
		<xsl:message>
			Number of tables: <xsl:value-of select="count(//CWMRDB:Table)"/>
		</xsl:message>
		<xsl:message>
			Number of classes : <xsl:value-of select="count(//UML:Class)" />
		</xsl:message>
		<table border="1">
			<xsl:apply-templates/>
		</table>
			</body>
		</html>
	</xsl:template>


	<!-- This transform summarizes in HTML an XMI 1.1 / UML 1.3 file
(from Unicorn or Rose add-in e.g.) 
-->

<xsl:template match="UML:Class/*/UML:Attribute" >
	<xsl:variable name="class-id" select="../../@xmi.id" />
	<xsl:variable name="attribute-id" select="@xmi.id/text()" />
	<tr><!-- Class -->
		<td><xsl:value-of select="../../@name" /></td>
		<td><xsl:value-of select="
				// UML:TaggedValue [ @tag = 'documentation' ] 
				[ @modelElement = $class-id ]
				/ UML:TaggedValue.value
				" /></td>
		<!-- Attribute -->
		<td><xsl:value-of select="      @name" /></td>
		<td> <xsl:apply-templates mode="documentation" select="$attribute-id" /> </td>
		<td><xsl:value-of select="      key('type', @type)/@name" /></td>
		<td><xsl:value-of select="      @type" /></td>
		</tr><xsl:text>
	</xsl:text>
</xsl:template>

<xsl:key name = "type"
   match = "//UML:DataType"
   use = "@xmi.id" />

<xsl:template mode="documentation" match='text()' >
<xsl:value-of select="
				// UML:TaggedValue [ @tag = 'documentation' ] 
				[ @modelElement = . ]
				/ UML:TaggedValue.value
				" />
</xsl:template>

<!--
<xsl:template match="UML:Class" >
	<tr>
		<td><xsl:value-of select="@name" /></td>
	<xsl:apply-templates mode="fields" />
		</tr><xsl:text>
	</xsl:text>
</xsl:template>

<xsl:template match="UML:Attribute" mode="fields" >
		<td><xsl:value-of select="@name" /></td>
</xsl:template>
-->


 <xsl:template match="text()" />

<xsl:template name="css">
<style TYPE="text/css">
<xsl:comment>
table {
	background:#D3E4E5;
	border:1px solid gray;
	border-collapse:collapse;
	color:#fff;
	font:normal 12px verdana, arial, helvetica, sans-serif;
	}
caption { border:1px solid #5C443A;
	color:#5C443A;
	font-weight:bold;
	letter-spacing:20px;
	padding:6px 4px 8px 0px;
	text-align:center;
	text-transform:uppercase;
	}
td, th { color:#363636;
	/*padding:.4em;*/
	}
tr { border:1px dotted gray;
	}
thead th, tfoot th { background:#5C443A;
	color:#FFFFFF;
	padding:3px 10px 3px 10px;
	text-align:left;
	text-transform:uppercase;
	}
tbody td a { color:#363636;
	text-decoration:none;
	}
tbody td a:visited { color:gray;
	text-decoration:line-through;
	}
tbody td a:hover {
	text-decoration:underline;
	}
tbody th a { color:#363636;
	font-weight:normal;
	text-decoration:none;
	}
tbody th a:hover { color:#363636;
	}
tbody td+td+td+td a { background-image:url('bullet_blue.png');
	background-position:left center;
	background-repeat:no-repeat;
	color:#03476F;
	padding-left:15px;
	}
tbody td+td+td+td a:visited { background-image:url('bullet_white.png');
	background-position:left center;
	background-repeat:no-repeat;
	}
tbody th, tbody td { text-align:left;
	vertical-align:top;
	}
tfoot td { background:#5C443A;
	color:#FFFFFF;
	padding-top:3px;
	}
.odd { background:#fff;
	}
tbody tr:hover {
	background:#99BCBF;
	border:1px solid #03476F;
	color:#000000;
	}
</xsl:comment>
</style>
</xsl:template>

</xsl:stylesheet>
